<template>
	<view class="">
		<u-navbar title="订单详情" placeholder autoBack></u-navbar>
		
		<view class="p-2">
			<block v-for="(item,index) in detail.goods_order" :key="index">
				<view class="border mb-2" style="border-radius: 16rpx;">
					<view class="bg-white p-2 brStyle">
						<orderItem :item="item" type="order" @click=""></orderItem>
					</view>
					<view class="bg-white brStyle p-2">
						<view class="flex my-2">
							<view class="mr-1">商品评价</view>
							<u-rate count="5" v-model="item.goods_star" activeColor="#feac12"></u-rate>
						</view>
						<u-textarea class="brStyle" v-model="item.comment" border="none" placeholder="评价内容..." height="160" style="background-color: #f6f6f6;"></u-textarea>
					</view>
					
					<view class="p-2 mt-2 bg-white brStyle">
						<view class="flex align-end">
							<view class="font-30">添加图片</view>
							<view class="font-24 ml-2" style="color: #cecece;">最多可上传9张</view>
						</view>
						<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="9">
							<u-image src="/static/user/addpic1.png" width="156rpx" height="156rpx"></u-image>
						</u-upload>
					</view>
				</view>	
			</block>
		</view>
		<view class="" style="height: 160rpx;"></view>
		<view style="position: fixed; bottom: 20rpx; right: 0;width: 100%;">
			<commonButton title="发布评论" @click="submint" background="#85b885"></commonButton>
		</view>
	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from '@/utils/image-tools.js'
	import {
		base
	} from '@/request/env.js'
	import {
		mapMutations,
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				fileList1: [],
				images: []
			}
		},
		onLoad() {
			console.log(this.detail)
			this.detail.goods_order = this.detail.goods_order.map(item => {
				return {
				    ...item,
					comment:'',
					goods_star: 5,
					imgTemp: [],
					imgpicture: []
				}
			})
		},
		computed: {
			...mapState({
				detail: state => state.order.orderDetial
			})
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				console.log(url)
				return new Promise((resolve, reject) => {
				    uni.uploadFile({
				        url: base + "/api/index/uploadData",
						method: "POST",
				        filePath: url,
				        name: "file",
				        formData: {
				            user: 'test',
							token:uni.getStorageSync('token')
				        },
				        success: (res) => {
				          //setTimeout(() => {
				          let data = JSON.parse(res.data)
				          console.log(data)
				          resolve(data.data.url)
				        },
				    })
				})
			},
			submint(){
				this.fileList1.forEach(item=>{
					this.images.push(item.url)
				})
				this.detail.goods_order = this.detail.goods_order.map(item=>{
					return {
						order_goods_id:item.id,
						comment:item.comment,
						goods_star: item.goods_star,
						img:this.images.join(',')
					}
				})
				
				
				for (let i = 0; i < this.detail.goods_order.length; i++) {
					let item = this.detail.goods_order[i]
					this.$api.addComment(item).then(res=>{
						if(res.code !== 1){
							return		
						}	
					})	
				}
				
				uni.showToast({
					title: '评价成功',
					icon:'none'
				});
				setTimeout(()=>{
					uni.navigateBack({
						delta: 1
					});
				},500)
				
			}
		}
	}
</script>

<style scoped>
   .brStyle {
	   border-radius: 24rpx;
   }
</style>
